<!--
 * Copyright 2013 The Chromium Authors. All rights reserved.  Use of this
 * source code is governed by a BSD-style license that can be found in the
 * LICENSE file.
-->
<html>
  <head>
    <script type="text/javascript">
      // A guest that requests a new window.
      // Notifies the embedder about the result of the request (success/fail)
      // via post message. Note that the embedder has to initiate a postMessage
      // first so that guest has a reference to the embedder's window.

      // The window reference of the embedder to send post message reply.
      var embedderWindowChannel = null;

      var notifyEmbedder = function(msg_array) {
        embedderWindowChannel.postMessage(JSON.stringify(msg_array), '*');
      };

      var openWindow = function(url, frameName) {
        window.open(url, frameName);
      };

      var openWindowBlankRedirect = function(url, frameName) {
        console.log('redirect');
        var w = window.open('', frameName);
        w.opener = null;
        w.document.write(
            '<META HTTP-EQUIV="refresh" content="0; url=' + url + '">');
        w.document.close();
      }

      var onPostMessageReceived = function(e) {
        embedderWindowChannel = e.source;
        var data = JSON.parse(e.data);
        if (data[0] == 'open-window') {
          var url = data[1];
          var frameName = data[2];
          var redirect = data[3];
          if (redirect) {
            openWindowBlankRedirect(url, frameName);
          } else {
            openWindow(url, frameName);
          }
        } 
      };

      window.addEventListener('message', onPostMessageReceived, false);
    </script>
  </head>
  <body>
    <div>This is a guest that requests a new window.</div>
  </body>
</html>
